package com.room.service;

import com.room.mapper.RepairitemsMapper;
import com.room.mapper.RepairlogMapper;
import com.room.pojo.Repairitems;
import com.room.pojo.Repairlog;
import com.room.service.RepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class RepairService {
    @Autowired
    private RepairlogMapper repairlogMapper;

    @Autowired
    private RepairitemsMapper repairitemsMapper;


    public List<Repairitems> findAllTbRepairitmes(Integer page, Integer size) {
        int start = (page - 1) * size;
        return repairitemsMapper.getRepairitems(start, size);
    }


    public List<Repairitems> findRepairitmeByRoomName(String name, Integer page, Integer size) {
        int start = (page - 1) * size;
        return repairitemsMapper.getRepairitemsByRoomname(name, start, size);
    }


    public int deleteRepairitems(int item_id, String worker_id) {
        Repairitems repairitems = repairitemsMapper.selectByPrimaryKey(item_id);
        Repairlog repairlog = new Repairlog();
        repairlog.setApartmentId(repairitems.getApartmentId());
        repairlog.setRoomName(repairitems.getRoomName());
        repairlog.setWorkerId(worker_id);
        repairlog.setBeforeDate(repairitems.getReptime());
        repairlog.setAfterDate(new Date());
        int i = repairitemsMapper.deleteByPrimaryKey(item_id);
        int insert = repairlogMapper.insertSelective(repairlog);
        if (i == 1 && insert == 1) {
            return 1;
        }
        return 0;
    }


    public int addRepairitems(Repairitems Repairitems) {
        return repairitemsMapper.insertSelective(Repairitems);
    }


    public int updateRepairitems(Repairitems Repairitems) {
        return repairitemsMapper.updateByPrimaryKeySelective(Repairitems);
    }


    public List<Repairlog> findAllRepairlog(Integer page, Integer size) {
        int start = (page - 1) * size;
        return repairlogMapper.getRepairlog(start,size);
    }


    public List<Repairlog> findRepairlogByWorkerId(String workerNo, Integer page, Integer size) {
        int start = (page - 1) * size;
        return repairlogMapper.getRepairlogByWorkerId(workerNo,start,size);
    }


    public int getRepairitemsCount() {
        return repairitemsMapper.getRepairitemsCount();
    }


    public int getRepairitemsCountByRoomName(String roomname) {
        return repairitemsMapper.getRepairitemsCountByroomname(roomname);
    }


    public int getRepairlogCount() {
        return repairlogMapper.getRepairlogCount();
    }

    public int getRepaitlogCountByWorkerId(String workerNo) {
        return repairlogMapper.getRepairlogCountByworkerId(workerNo);
    }
}

